package com.duty.UI;

import java.text.DecimalFormat;

import com.duty.UI.R;
import com.duty.variables.AlcoholVariables;
import com.duty.variables.TobaccoVariables;
import com.exchange.rate.MessageList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class TobaccoActivity extends Activity {

	private String province;
	private String amountType;
	private double amount = 0;
	private double totalCost = 0;
	DecimalFormat df = new DecimalFormat("0.00");
	AlcoholVariables variables = new AlcoholVariables();
	TobaccoVariables tobacco = new TobaccoVariables();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        //retrieve info from last activity
        Bundle extras = getIntent().getExtras();
        province = extras.getString("province");
               
        setContentView(R.layout.tobacco);
                     
		//create the spinner to hold the list of Class events that can be added
		Spinner amountSpinner = (Spinner) findViewById(R.id.amount_box);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.CigaretteArray, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		amountSpinner.setAdapter(adapter);

		// Set up a call back for the spinner
		amountSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onNothingSelected(AdapterView<?> arg0) {
				amount = 0;
			}
			
			public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
				final Spinner amountEvent = (Spinner) findViewById(R.id.amount_box);
				amountType = amountEvent.getSelectedItem().toString();
				
				if(amountType.equals("1 Carton (200 Cigarettes)")){
					amount = 200;
				}
				else if(amountType.equals("1 Pack (20 Cigarettes)")){
					amount = 20;
				}			
 			}
		});
        
		Button btnSubmit = (Button) findViewById(R.id.submit_duty);
		btnSubmit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				
			EditText cost = (EditText)findViewById(R.id.cost_box);	
			String tempCost = cost.getText().toString();
			if (cost.length() > 0){
				totalCost = Double.parseDouble(cost.getText().toString());
			}
			
			
//		    final CheckBox checkUS = (CheckBox) findViewById(R.id.checkbox);
//		    		    
//			if (checkUS.isChecked()) {     	              
//          	   //calculate conversion to CAD
//				   
//			    // get currency rate using xml feed  1USD -> 1CAD
//			    MessageList currency = new MessageList();		    
//			    String strConversion = currency.loadFeed();
//			    String delims = "[()]";
//			    String[] tokens = strConversion.split(delims);
//			    double currencyRate = Double.valueOf(tokens[1].trim()).doubleValue();
//			    				    
//			    totalCost = totalCost * currencyRate;
//            }
			   
			if( tempCost.length() == 0) {	

	    		   AlertDialog alertDialog = new AlertDialog.Builder(TobaccoActivity.this).create();	        			
	    		   alertDialog.setTitle("Warning");	        		  
	    		   alertDialog.setMessage("Please fill in all fields");	        		  	        		  
	    		   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
	    		  
	    		   public void onClick(DialogInterface dialog, int which) {
	    			   return;
	    		   } }); 
		
		           alertDialog.show();		  
		     }
						   
			TextView txtTotal=(TextView)findViewById(R.id.total);
			 			 
				if (province.equals("Alberta")){  					   
			             txtTotal.setText( calculateAlberta(totalCost,amount));           
			        } 
			        else if (province.equals("British Columbia")){       		
			             txtTotal.setText( calculateBritishColumbia(totalCost,amount));                 
			        }
			        else if (province.equals("Manitoba")){       		
			             txtTotal.setText( calculateManitoba(totalCost,amount));                
			         }
			        else if (province.equals("New Brunswick")){       		
			             txtTotal.setText( calculateNewBrunswick(totalCost,amount));     
			         }
			        else if (province.equals("Newfoundland")){       		
			        	 txtTotal.setText( calculateNewfoundland(totalCost,amount));                  
			         }
			        else if (province.equals("Nova Scotia")){       		
			        	 txtTotal.setText( calculateNovaScotia(totalCost,amount));                  
			         }
			        else if (province.equals("Ontario")){       		
			        	 txtTotal.setText( calculateOntario(totalCost,amount));                 
			         }
			        else if (province.equals("P.E.I.")){       		
			        	 txtTotal.setText( calculatePEI(totalCost,amount));
			        }
			        else if (province.equals("Quebec")){       		
			        	txtTotal.setText( calculateQuebec(totalCost,amount));      
			         }
			        else if (province.equals("Saskatchewan")){       		
			        	 txtTotal.setText( calculateSaskatchewan(totalCost,amount));           
			         }
			        else if (province.equals("Yukon")){       		
			        	 txtTotal.setText( calculateYukon(totalCost,amount));                 
			         }								
			}
		});       
        
    }
    
private String calculateAlberta(double cost, double amount){	
		
	double excise_duty = exciseDuty(amount);		
	double provincial_markup = amount * tobacco.getAlbertaMarkup(); 
	double gst_tax = (cost + provincial_markup + excise_duty) * variables.getGST();
	double total = 0;
	
	total = provincial_markup + excise_duty + gst_tax;
	
    return "Total Duty $" + df.format(total);
}

private String calculateBritishColumbia(double cost, double amount){

	double excise_duty = exciseDuty(amount);
	double sales_tax = (cost + excise_duty) * variables.getBCHST();
	double provincial_markup = amount * tobacco.getBCMarkup(); 
	double total = 0;
					
	total = provincial_markup + excise_duty + sales_tax  ;
	
	return "Total Duty $" + df.format(total);
}

private String calculateManitoba(double cost, double amount){	

	double excise_duty = exciseDuty(amount);		
	double provincial_markup = amount * tobacco.getManitobaMarkup();
	double gst_tax = (cost + excise_duty) * variables.getGST(); 
	double provincial_tax = (cost + provincial_markup + excise_duty) * variables.getManitobaPST();	
	double total = 0;
	
	total = provincial_markup + excise_duty + gst_tax + provincial_tax;		
	
	return "Total Duty $" + df.format(total);
}

private String calculateNewBrunswick(double cost, double amount){	
	
	double excise_duty = exciseDuty(amount);
	double sales_tax = (cost + excise_duty) * variables.getNewBrunswickHST();
	double provincial_markup = amount * tobacco.getNBMarkup();  
	
	double total = 0;	
		
	total = sales_tax + excise_duty + provincial_markup;
	
	return "Total Duty $" + df.format(total);
}

private String calculateNewfoundland(double cost, double amount){	
	
//	double excise_duty = exciseDuty(amount);
//	double provincial_markup = (amount/28.4) * tobacco.getNfldMarkup(); //calculate oz	
//	double sales_tax = (cost + provincial_markup + excise_duty) * variables.getNfldHST();
//	double total = 0;	
		
//	total = provincial_markup + excise_duty + sales_tax;
	
    return "For Nfld., P.E.I., Nova Scotia," +
    " and Territories, please contact local " + 
    "authorities";
    		
}

private String calculateNovaScotia(double cost, double amount){	
	
//	double excise_duty = exciseDuty(amount);		
//	double provincial_markup = (amount/28.4) * variables.getNovaScotiaBeerMarkup(); //calculate oz
//	double hst_tax = (cost + provincial_markup + excise_duty) * variables.getNovaScotiaHST();
//	double total = 0;
	
//	total = provincial_markup + excise_duty + hst_tax;
	
    return "For Nfld., P.E.I., Nova Scotia," +
    " and Territories, please contact local " + 
    "authorities";
}

private String calculateOntario(double cost, double amount){	
	
	double excise_duty = exciseDuty(amount);
	double hst_tax = (cost + excise_duty) * variables.getOntarioHST();
	double provincial_markup = amount * tobacco.getOntarioMarkup(); 
	double total = 0;	
		
	total = provincial_markup  + excise_duty + hst_tax;
	
	return "Total Duty $" + df.format(total);
}

private String calculatePEI(double cost, double amount){

//	double excise_duty = exciseDuty(amount);
//	double provincial_markup = (amount/28.4) * variables.getPeiBeerMarkup(); //calculate oz
//	double gst_tax = (cost + provincial_markup + excise_duty) * variables.getGST();	
//	double provincial_tax = (cost + excise_duty + provincial_markup) * variables.getPeiPST();
//	double total = 0;
	
//	total = provincial_markup + excise_duty + gst_tax + provincial_tax;
   
	return "For Nfld., P.E.I., Nova Scotia," +
    " and Territories, please contact local " + 
    "authorities";
}

private String calculateQuebec(double cost, double amount){

	double excise_duty = exciseDuty(amount);
	double provincial_markup = amount * tobacco.getQuebecMarkup();
	double gst_tax = (cost + excise_duty) * variables.getGST();
	double provincial_tax = (cost + provincial_markup) * variables.getQuebecPST();
	double total = 0;	
		
	total = provincial_markup  + excise_duty + gst_tax + provincial_tax;
	
	return "Total Duty $" + df.format(total);
}

private String calculateSaskatchewan(double cost, double amount){

	double excise_duty = exciseDuty(amount);
	double provincial_markup = amount  * tobacco.getSaskMarkup(); 
	double gst_tax = (cost + excise_duty) * variables.getGST();
	double total = 0;	
		
	total = provincial_markup + excise_duty + gst_tax;

	return "Total Duty $" + df.format(total);
}

private String calculateYukon(double cost, double amount){

//	double excise_duty = exciseDuty(amount);
//	double provincial_markup = amount * tobacco.getYukonMarkup(); 
//	double gst_tax = (cost + provincial_markup + excise_duty) * variables.getGST();	
//	double total = 0;	
	
//	total = provincial_markup + excise_duty + gst_tax;

	return "For Nfld., P.E.I., Nova Scotia," +
    " and Territories, please contact local " + 
    "authorities";
}

private Double exciseDuty(double amount){

	double excise_duty = (amount/5) * tobacco.getCigaretteExcise();
	
	return excise_duty;
	    	
}

}



